package com.app.couponsharelife.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.LifeShopPageBean
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.databinding.LocalShopDetailCouponItemBinding
import com.app.couponsharelife.databinding.LocalShopDetailInfoItemBinding
import com.app.couponsharelife.databinding.LocalShopDetailShopItemBinding
import com.app.couponsharelife.ui.listener.OnClickListener

class LocalShopDetailAdapter(val list: MutableList<Any>,
                             private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    fun setData(data: LifeShopPageBean.RecordsBean) {
        list.clear()
        if (data.cards!!.isNotEmpty())
            list.add(data.cards!!)
        list.add(data.shop)
        list.add(data.info)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            1 -> ViewHolderCoupon(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.local_shop_detail_coupon_item, parent, false))
            2 -> ViewHolderShop(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.local_shop_detail_shop_item, parent, false))
            else -> ViewHolderInfo(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.local_shop_detail_info_item, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return when {
            list[position] is PageTipBean -> 0
            list[position] is LifeShopPageBean.InfoBean -> 3
            list[position] is LifeShopPageBean.ShopBean -> 2
            else -> 1
        }
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        when {
            getItemViewType(position) == 0 -> (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
            getItemViewType(position) == 1 -> {
                val layoutManager = LinearLayoutManager(holder.itemView.context)
                layoutManager.orientation = LinearLayoutManager.HORIZONTAL
                (holder as ViewHolderCoupon).databinding.mRecyclerView.layoutManager = layoutManager
                holder.databinding.mRecyclerView.adapter =
                        LocalShopDetailCouponAdapter(list[position] as List<LifeShopPageBean.CardsBean>, object : OnClickListener {
                            override fun click(arg1: Int, arg2: Any) {
                                listener.click(arg1, arg2)
                            }
                        })
            }
            getItemViewType(position) == 2 -> {
                val shopBean = list[position] as LifeShopPageBean.ShopBean
                (holder as ViewHolderShop).databinding.tvShopOpenTime.text = String.format("营业时间：%s", shopBean.openTime)
                holder.databinding.tvShopOwner.text = String.format("联系人：%s", shopBean.people)
                holder.databinding.tvShopMobile.text = String.format("联系电话：%s", shopBean.phone)
                holder.databinding.tvShopAddress.text = String.format("门店地址：%s", shopBean.address)
                holder.databinding.tvShopPhone.text = String.format("门店电话：%s", shopBean.telephone)
            }
            else -> {
                val infoBean = list[position] as LifeShopPageBean.InfoBean
                (holder as ViewHolderInfo).databinding.mWebView.loadDataWithBaseURL(null,
                        String.format("<div style='width:200px;color:red;display:block;word-break: break-all;word-wrap: break-word'> %s </div> %s ",
                                "<style type=\"text/css\"> img { width:100%; height:auto;} </style>",
                                infoBean.info.replace("<h4","<div").replace("</h4>","</div>")), "text/html", "utf-8", null)
                holder.databinding.tvTitle.text = infoBean.name
            }
        }
    }

    inner class ViewHolderCoupon(val databinding: LocalShopDetailCouponItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root)

    inner class ViewHolderShop(val databinding: LocalShopDetailShopItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root)

    inner class ViewHolderInfo(val databinding: LocalShopDetailInfoItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root)
}